/**
 *
 * 前台菜单增加类
 * @author lgc
 * 
 */
package com.sysmenu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import net.sf.json.JSONArray;
import com.common.util.BaseAction;
import com.pojo.SysmenuPojo;
import com.sysmenu.service.SysmenuService;

public class SysmenuAction extends BaseAction {

	public static Logger log = Logger.getLogger(SysmenuAction.class);
	private static final long serialVersionUID = -3106714864116301911L;
	private String name;
	private String link;
	private int orderNum;
	private SysmenuService sysmenuService;
	private int page;
	private int rows;
	private int total;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public SysmenuService getSysmenuService() {
		return sysmenuService;
	}

	public void setSysmenuService(SysmenuService sysmenuService) {
		this.sysmenuService = sysmenuService;
	}

	/**
	 * 保存前台菜单
	 * 
	 * @return
	 */
	public String addSaveSysmenu() {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("name", name);
		paraMap.put("link", link);
		paraMap.put("orderNum", orderNum);
		sysmenuService.saveSysmenu(paraMap);
		return "success";
	}

	public String searchMenulist() {
		Map<String, Object> paraMap = new HashMap<String, Object>();

		int start = (page - 1) * rows;
		int end = page * rows;
		paraMap.put("start", start);
		paraMap.put("end", end);
		List<SysmenuPojo> list = sysmenuService.searchMenuList(paraMap);
		int count = sysmenuService.searchMenuCount();
		total = count;
		StringBuffer resultMsg = new StringBuffer("{");
		resultMsg.append("\"total\":");
		resultMsg.append(count);
		String menulist = JSONArray.fromObject(list).toString();
		resultMsg.append(",\"rows\":");
		resultMsg.append(menulist);
		resultMsg.append("}");
		try {
			PrintWriter out = response.getWriter();
			response.setCharacterEncoding("utf-8");
			out.write(resultMsg.toString());

		} catch (IOException e) {
			log.error("search menulist error", e);
		}
		return null;
	}
	public String deleteByIds() throws IOException {
		try {
			String ids = request.getParameter( "ids");
			boolean isFlag =sysmenuService.deleteByIdString(ids);
			response.setContentType("text/html;charset=utf-8");
			if (isFlag)
				message = "删除成功";
			else
				message = "删除失败";
			StringBuffer sb = new StringBuffer("[");
			sb.append("{\"message\":\"" + message + "\"" + "}");
			sb.append("]");
			this.response.getWriter().print(sb.toString());
		} catch (Exception e) {
			log.error("delete menulist error", e);
		}
		return null;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

}
